#ifndef _______________WARD_LOGICPlANET_H
#define _______________WARD_LOGICPlANET_H

#include "driver.h"
#include "Force.h"

#define NEUTRAL -1

#define INCREASE 1.005
#define DECREASE 0.90


class logicPlanet
{
	public:
		logicPlanet(string n) {name = n; owner = NEUTRAL; population = (rand()%15000)+4000; battleready = (rand()%5)+1;}

		void initialize(int o, int p, int b) { owner = o; population = p; battleready = b;}

		Force& getForce() { return contingent; }	
		int getPop() { return population; }
		int getOwner() { return owner; }
		string getRdyStr() { return readydescriptors[battleready-1]; }
		int getRdy() { return battleready; }
		string getName() { return name;}
		void train() 
		{
			//trains the planet to increase its battle readiness
			if(rand()%50  == 49) { if(battleready < 5) battleready++; }
			int n = (contingent.getSize()*0.4)+1; contingent.train(n);
		}

		void setForce(Force f) { contingent = f; }
		void setOwner(int n) { owner = n; contingent.setOwner(owner);}
		void setRdy(int b) { battleready = b;}


		void turn() { if(owner != NEUTRAL) {increasePopulation(); contingent.add(max(1,(int)(population*0.0003)), battleready);} }

		void decreasePopulation() { population *= DECREASE; }
		void increasePopulation() { population *= INCREASE; }


		void print() {
			cout << "---------------------------------------" << endl
				 << "Name: " << name << endl
				 << "Owner: " << owner << endl
				 << "Population: " << population << endl
				 << "BattleReady: " << battleready << endl;
			cout << "Force: " << contingent.getSize() << endl;

					cout << "---------------------------------------" << endl;
		}

		vector <string> infoStrings() {
			const int buf_size =45;
			vector <string> ret;
			char* buffer = new char[buf_size];

			ret.push_back("Name: " + name);
			_itoa_s(owner+1, buffer, buf_size, 10);
			string owned(buffer);
			if(owned == "0") owned = "Unowned";
			else owned = "Player " + owned;
			ret.push_back("Owner: " + owned);
			_itoa_s(population, buffer,buf_size, 10);
			ret.push_back("Population: " + string(buffer));
			ret.push_back(string("Planet Readiness: ") + readydescriptors[battleready-1]);
			_itoa_s(contingent.getSize(), buffer,buf_size, 10);
			ret.push_back("Force: " + string(buffer));
			if(contingent.getSize() > 0)
			ret.push_back(string("Force Readiness: ") + readydescriptors[contingent.getRdy()-1]);
			else
			ret.push_back(string("Force Readiness: ") + readydescriptors[0]);

			return ret;
		}

	private:
		int population;
		int battleready;
		Force contingent;
		int owner;
		string  name;

};

//Attack
//Executes an attack and returns the winner ownerID
//
static bool attack(Force &attacker, logicPlanet &defender, bool aShip, bool dShip)
{
	int winner = -1;

	if(attacker.getOwner() != defender.getOwner())
	{
		winner = attack(attacker,defender.getForce(), aShip, dShip);
		
		defender.decreasePopulation();

		if(winner == attacker.getOwner())		//attackers win, take planet
		{
			char buf [5];
			_itoa_s(attacker.getOwner()+1,buf,5,10);
			myMSG.add("Player "+string(buf)+" has taken "+defender.getName()+"!");

			defender.setOwner(attacker.getOwner());	//take planet
			defender.setForce(attacker);		//attackers become the planet's contingent
			defender.setRdy(max(1,defender.getRdy() - 1));
		
		}
		else									//defenders win
		{
			char buf [5];
			_itoa_s(defender.getOwner()+1,buf,5,10);
			myMSG.add("Player "+string(buf)+" has defended "+defender.getName()+"!");
			if(rand()%20 == 0) 	defender.setRdy(defender.getRdy() + 1);
		}
	}
	else
	{
			char buf[5],buf2[5];
			_itoa_s(defender.getForce().getSize(),buf,5,10);
			_itoa_s(attacker.getSize(),buf2,5,10);
			myMSG.add(string(buf2) + " troops arrive to reinforce " + defender.getName() +"!");

			merge(defender.getForce(),attacker,attacker.getSize());
			winner = attacker.getOwner();
	}
	if(winner == attacker.getOwner()) return true;
	else return false;
}


#endif